package com.ruoyi.business.modbus.domain;

import java.io.Serializable;

import com.serotonin.modbus4j.code.DataType;

/**
 * Modbus读取请求对象
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
public class ModbusReadRequest implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 请求ID */
    private String requestId;

    /** 功能码 (HOLDING_REGISTERS, INPUT_REGISTERS, COILS, DISCRETE_INPUTS) */
    private String functionCode;

    /** 起始地址 */
    private int startAddress;

    /** 数量 */
    private int quantity;

    /** 数据类型 (TWO_BYTE_INT_UNSIGNED, FOUR_BYTE_FLOAT, etc.) */
    private int dataType;

    /** 从站地址 */
    private int slaveAddress;

    /** 描述 */
    private String description;

    public ModbusReadRequest() {}

    public ModbusReadRequest(String requestId, String functionCode, int slaveAddress, int startAddress, int quantity) {
        this.requestId = requestId;
        this.functionCode = functionCode;
        this.slaveAddress = slaveAddress;
        this.startAddress = startAddress;
        this.quantity = quantity;
        this.dataType = DataType.TWO_BYTE_INT_UNSIGNED;
    }

    public ModbusReadRequest(String requestId, String functionCode, int slaveAddress, int startAddress, int quantity, int dataType) {
        this(requestId, functionCode, slaveAddress, startAddress, quantity);
        this.dataType = dataType;
    }



    // 静态工厂方法

    public static ModbusReadRequest holdingRegisters(String requestId, int slaveAddress, int startAddress, int quantity) {
        return new ModbusReadRequest(requestId, "HOLDING_REGISTERS", slaveAddress, startAddress, quantity, DataType.TWO_BYTE_INT_UNSIGNED);
    }


    public static ModbusReadRequest inputRegisters(String requestId, int slaveAddress, int startAddress, int quantity) {
        return new ModbusReadRequest(requestId, "INPUT_REGISTERS", slaveAddress, startAddress, quantity, DataType.TWO_BYTE_INT_UNSIGNED);
    }

    public static ModbusReadRequest coils(String requestId, int slaveAddress, int startAddress, int quantity) {
        return new ModbusReadRequest(requestId, "COILS", slaveAddress, startAddress, quantity, DataType.TWO_BYTE_INT_UNSIGNED);
    }


    public static ModbusReadRequest discreteInputs(String requestId, int slaveAddress, int startAddress, int quantity) {
        return new ModbusReadRequest(requestId, "DISCRETE_INPUTS", slaveAddress, startAddress, quantity, DataType.TWO_BYTE_INT_UNSIGNED);
    }

    // Getters and Setters
    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public String getFunctionCode() {
        return functionCode;
    }

    public void setFunctionCode(String functionCode) {
        this.functionCode = functionCode;
    }

    public int getStartAddress() {
        return startAddress;
    }

    public void setStartAddress(int startAddress) {
        this.startAddress = startAddress;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getDataType() {
        return dataType;
    }

    public void setDataType(int dataType) {
        this.dataType = dataType;
    }

    public int getSlaveAddress() {
        return slaveAddress;
    }

    public void setSlaveAddress(int slaveAddress) {
        this.slaveAddress = slaveAddress;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "ModbusReadRequest{" +
                "requestId='" + requestId + '\'' +
                ", functionCode='" + functionCode + '\'' +
                ", startAddress=" + startAddress +
                ", quantity=" + quantity +
                ", dataType=" + dataType +
                ", slaveAddress=" + slaveAddress +
                ", description='" + description + '\'' +
                '}';
    }
}
